package GUI;

import java.awt.Color;
import javax.swing.JOptionPane;
import Client_logic.ClientNotInitializesException;
import Client_logic.Category;

/**
 * This class use for add type to category.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil*
 */
public class AddTypeToCategoryForm extends TypeManagementForm {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private boolean addflag;
	private boolean removeflag;
	private Category category = null; 
	
	public AddTypeToCategoryForm(){
		super();
		initialize(); 
	}
	
	/**
	 * This method initializes CustomerForm
	 */
	private void initialize() {
		category = new Category();		
		addflag = false;
		removeflag = false;
		updateSectionsIDComboBox();
		getSaveButton().setText("Add");	
		getIdLabel().setText("Section ID");
		getNameLabel().setText("Type ID");
		getTypesIDLabel().setText("All types");
		getProductsIDLabel().setText("All types in category");
		getAddbuttonLabel().setText("Selected info:");
		getNameTextField().setEditable(false);
		getIdTextField().setEditable(false);
		getUpdateButton().setEnabled(false);
		addSaveButton();
		addremoveButton();
		updateForm();
		addSectionsIDComboBox();
		
		getTypesIDComboBox().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {			
				String str = (String)getTypesIDComboBox().getSelectedItem();
				if (str.equals(":"))
					return;
				String typeID = str.substring(0, str.indexOf(":"));
				getNameTextField().setText(typeID);				
			}
		});
	}
	
	/**
	 * This method update form comboBoxes to data from data base
	 */
	public void updateForm(){
		updateTypesIDComboBox();
		updateSectionsIDComboBox();
	}
	
	/**
	 * This method initializes sectionsIDComboBox ActionListener
	 */
	private void addSectionsIDComboBox(){
		getSectionsIDComboBox().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {	
				int categoryID = 0;
				String str = (String)getSectionsIDComboBox().getSelectedItem();
				try{
					categoryID = Integer.parseInt(str.substring(0, str.indexOf(":")));
				}
				catch( NumberFormatException exc){
					return;
				}
				if (categoryID > 0)
					getIdTextField().setText(String.valueOf(categoryID));
				else	{
					getIdTextField().setText("");
					return;
				}
				category.SetCategoryID(categoryID);
				String[] typelist = null;
				try{
					typelist = category.GetAllCategoryTypes();
					if (typelist == null){
						getProductsIDTextArea().setText("");
					}							
					else{
						String tmpstr = "";
						int len = typelist.length;
						for(int i=0; i < len; i++){
							tmpstr = tmpstr+(typelist[i]+"\n");
						}
						getProductsIDTextArea().setText(tmpstr);
					}
				}
				catch(ClientNotInitializesException exc){
					JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}
				catch(Exception except){
					JOptionPane.showMessageDialog(null, "SYSTEM ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}				
			}
		});	
	}
	
	/**
	 * This method initializes SaveButton ActionListener
	 */
	private void addSaveButton(){
		getSaveButton().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {			
				if (addflag){
					addtypeToSection();										
				}
				else{
					getSaveButton().setText("OK");
					getAddbuttonLabel().setText("Add this type to the category:");
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					getRemoveButton().setEnabled(false);
					addflag = true;
				}
			}		
		});		
	}
	
	/**
	 * This method initializes removeButton ActionListener
	 */
	private void addremoveButton(){
		getRemoveButton().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {	
				if (removeflag){
					removeTypeFromCategory();										
				}
				else{
					getSaveButton().setText("OK");
					getAddbuttonLabel().setText("Remove this type from the category:");
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					getSaveButton().setEnabled(false);
					removeflag = true;
				}
			}
		});		
	}
	
	/**
	 * This method update selected category
	 */
	private void removeTypeFromCategory(){
		int typeID = 0;
		int categoryID = 0;
		try{
			categoryID = Integer.parseInt(getIdTextField().getText());
			typeID = Integer.parseInt(getNameTextField().getText());
		}
		catch(NumberFormatException exception){
			JOptionPane.showMessageDialog(null, "PLZ select a category/type","",JOptionPane.WARNING_MESSAGE);
			return;
		}	
		
		if (categoryID > 0 && typeID > 0){
			category.SetCategoryID(categoryID);
			try {
				if (category.removeTypeFromCategory(typeID)){
					JOptionPane.showMessageDialog(null, "Type: "+typeID+" successfuly remove from category: "+categoryID);
					restore();			
				}
				else
					JOptionPane.showMessageDialog(null, "Type not remove!!!","",JOptionPane.WARNING_MESSAGE);
			} 
			catch (ClientNotInitializesException e1) {
				JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			}						
		}
		else
			JOptionPane.showMessageDialog(null, "PLZ select a category/type","",JOptionPane.WARNING_MESSAGE);
	}
	
	
	/**
	 * This method add selected type to the category 
	 */
	private void addtypeToSection(){
		int typeID = 0;
		int categoryID = 0;
		try{
			categoryID = Integer.parseInt(getIdTextField().getText());
			typeID = Integer.parseInt(getNameTextField().getText());
		}
		catch(NumberFormatException exception){
			JOptionPane.showMessageDialog(null, "PLZ select a category","",JOptionPane.WARNING_MESSAGE);
			return;
		}	
		
		if (categoryID > 0 && typeID > 0){
			category.SetCategoryID(categoryID);
			try {
				if (category.addTypeToCategory(typeID)){
					JOptionPane.showMessageDialog(null, "Type: "+typeID+" successfuly added to category: "+categoryID);
					restore();
				}
				else
					JOptionPane.showMessageDialog(null, "Type not added!!!","",JOptionPane.WARNING_MESSAGE);
			} 
			catch (ClientNotInitializesException e1) {
				JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			}						
		}
		else{
			JOptionPane.showMessageDialog(null, "PLZ select a category","",JOptionPane.WARNING_MESSAGE);
			restore();
		}
	}	
	
	/**
	 * This method restore the form button  
	 */
	public void restore(){
		getSaveButton().setText("Add");	
		getAddbuttonLabel().setText("Selected info:");	
		addflag = false;
		removeflag = false;
		getIdTextField().setForeground(Color.black);
		getNameTextField().setForeground(Color.black);
		getAddbuttonLabel().setForeground(Color.black);
		getIdTextField().setText("");
		getNameTextField().setText("");
		getRemoveButton().setEnabled(true);
		getSaveButton().setEnabled(true);		
	}
}
